const AuthService = require('../services/authService')
const logger = require('../utils/logger')
const { HTTP_STATUS } = require('../utils/constants')
const { asyncHandler } = require('../middleware/errorHandler')

class AuthController {
  /**
   * 用户注册
   */
  static register = asyncHandler(async (req, res) => {
    const { username, email, password, role } = req.body
    
    const result = await AuthService.register({
      username,
      email,
      password,
      role
    })
    
    res.status(HTTP_STATUS.CREATED).json({
      success: true,
      message: '注册成功',
      data: {
        user: result.user,
        tokens: result.tokens
      }
    })
  })
  
  /**
   * 用户登录
   */
  static login = asyncHandler(async (req, res) => {
    const { username, password } = req.body
    
    const result = await AuthService.login({
      username,
      password
    })
    
    res.json({
      success: true,
      message: '登录成功',
      data: {
        user: result.user,
        tokens: result.tokens
      }
    })
  })
  
  /**
   * 刷新访问令牌
   */
  static refreshToken = asyncHandler(async (req, res) => {
    const { refreshToken } = req.body
    
    const result = await AuthService.refreshToken(refreshToken)
    
    res.json({
      success: true,
      message: '令牌刷新成功',
      data: {
        user: result.user,
        tokens: result.tokens
      }
    })
  })
  
  /**
   * 用户登出
   */
  static logout = asyncHandler(async (req, res) => {
    const userId = req.user.id
    const token = req.token
    
    await AuthService.logout(userId, token)
    
    res.json({
      success: true,
      message: '登出成功'
    })
  })
  
  /**
   * 修改密码
   */
  static changePassword = asyncHandler(async (req, res) => {
    const userId = req.user.id
    const { currentPassword, newPassword } = req.body
    
    await AuthService.changePassword(userId, {
      currentPassword,
      newPassword
    })
    
    res.json({
      success: true,
      message: '密码修改成功'
    })
  })
  
  /**
   * 请求密码重置
   */
  static requestPasswordReset = asyncHandler(async (req, res) => {
    const { email } = req.body
    
    await AuthService.requestPasswordReset(email)
    
    res.json({
      success: true,
      message: '如果邮箱存在，重置链接已发送'
    })
  })
  
  /**
   * 重置密码
   */
  static resetPassword = asyncHandler(async (req, res) => {
    const { token, newPassword } = req.body
    
    await AuthService.resetPassword(token, newPassword)
    
    res.json({
      success: true,
      message: '密码重置成功'
    })
  })
  
  /**
   * 获取当前用户信息
   */
  static getCurrentUser = asyncHandler(async (req, res) => {
    const user = req.user
    
    res.json({
      success: true,
      data: {
        user
      }
    })
  })
  
  /**
   * 验证令牌
   */
  static verifyToken = asyncHandler(async (req, res) => {
    const { token } = req.body
    
    const result = await AuthService.verifyAccessToken(token)
    
    res.json({
      success: true,
      message: '令牌有效',
      data: {
        user: result.user,
        decoded: {
          userId: result.decoded.userId,
          username: result.decoded.username,
          role: result.decoded.role,
          exp: result.decoded.exp,
          iat: result.decoded.iat
        }
      }
    })
  })
  
  /**
   * 获取用户权限
   */
  static getUserPermissions = asyncHandler(async (req, res) => {
    const user = req.user
    
    const permissions = AuthService.getUserPermissions(user)
    
    res.json({
      success: true,
      data: {
        permissions
      }
    })
  })
  
  /**
   * 检查特定权限
   */
  static checkPermission = asyncHandler(async (req, res) => {
    const user = req.user
    const { permission } = req.params
    
    const hasPermission = AuthService.hasPermission(user, permission)
    
    res.json({
      success: true,
      data: {
        permission,
        hasPermission
      }
    })
  })
}

module.exports = AuthController